<script setup lang="ts">
import {useRoute} from 'vue-router'
import {useSystemStore} from "@/store/modules/systemStore/systemStore";
import {SystemStoreEnum, SystemStoreUserInfoEnum} from '@/store/modules/systemStore/systemStore.d'
import {getUserProfileApi} from '@/api/path'
import {onMounted} from "vue";
import { routerTurnByName } from '@/utils'
import { PageEnum } from '@/enums/pageEnum'

const systemStore = useSystemStore()
const route = useRoute()

onMounted(async ()=>{
  // ① Token 信息（先存储下，保证可以加载个人信息）
  const tokenValue = route.query.at as string
  const tokenName = 'Authorization'
  systemStore.setItem(SystemStoreEnum.USER_INFO, {
    [SystemStoreUserInfoEnum.USER_TOKEN]: tokenValue,
    [SystemStoreUserInfoEnum.TOKEN_NAME]: tokenName
  })

  const userId = route.query.ui as string
  // 个人信息
  const profileRes = await getUserProfileApi()
  const id = Number.parseInt(userId)
  const username = profileRes?.data?.nickname
  const nickname = profileRes?.data?.nickname
  // 存储到 pinia
  systemStore.setItem(SystemStoreEnum.USER_INFO, {
    [SystemStoreUserInfoEnum.USER_TOKEN]: tokenValue,
    [SystemStoreUserInfoEnum.TOKEN_NAME]: tokenName,
    [SystemStoreUserInfoEnum.USER_ID]: id,
    [SystemStoreUserInfoEnum.USER_NAME]: username,
    [SystemStoreUserInfoEnum.NICK_NAME]: nickname,
  })

  window['$message'].success('成功')
  const url = route.query.u as string
  window.location.replace(url)
})

</script>

<template>
<div>
  加载中
</div>
</template>

<style scoped lang="scss">

</style>